<template>
	<view class="content">
		<image style="width: 700rpx;height: 700rpx;margin-bottom: 20%;" :src="logoUrl"></image>
		<!-- <view class="title">申请获取以下权限</view>
		<text class="msg">获取你的公开信息（手机号码等）</text> -->
		<button class="btn" open-type="getPhoneNumber" @getphonenumber="handleCombinedLogin" :loading="isLoading">
			手机号快捷登录
		</button>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				logoUrl: 'https://www.taotaohomecraft.press/prod-api/profile/tao_logo copy.jpg',
				isLoading: false,
				loginCode: '', // 静默登录凭证
				phoneCode: '', // 手机号授权凭证
				sessionKey: null // 缓存服务端返回的密钥
			}
		},
		onLoad() {
			this.silentLogin();
		},
		methods: {
			async silentLogin() {
				try {
					const res = await uni.login({
						provider: 'weixin'
					});
					console.log(res)
					if (res[1].code) {
						this.loginCode = res[1].code; // 存储到data
					}
				} catch (err) {
					uni.showToast({
						title: '登录初始化失败',
						icon: 'none'
					});
				}
			},
			// 组合登录处理
			async handleCombinedLogin(e) {
				if (this.isLoading) return;
				this.isLoading = true;
				try {
					// 2. 校验参数
					if (!this.loginCode) throw new Error('获取登录凭证失败');
					if (!e.detail.code) throw new Error('需要手机号授权才能登录');

					// 3. 合并请求参数
					const params = {
						loginCode: this.loginCode,
						phoneCode: e.detail.code,
						platform: 'mp_weixin'
					}
					// 4. 调用登录接口
					await this.$store.dispatch('Login', params);
					this.loginSuccess();

				} catch (error) {
					console.error('登录失败:', error);
					uni.showToast({
						title: error.message || '登录失败',
						icon: 'none'
					});

				} finally {
					this.isLoading = false;
				}
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/home/index')
				})
			}
		}
	}
</script>

<style lang="less">
	/* 保持原有样式不变 */
	.content {
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		// justify-content: center;
		width: 100%;
		height: 100vh;
		// padding: 160rpx 40rpx 0;

		image {
			width: 275rpx;
			height: 104rpx;
		}

		.title {
			width: 100%;
			margin-top: 80rpx;
			margin-left: 10%;
			font-size: 30rpx;
			font-weight: 600;
			color: rgba(0, 0, 0, 0.85);
			text-align: left;
		}

		.msg {
			margin-left: 10%;
			display: block;
			width: 100%;
			margin-top: 16rpx;
			font-size: 28rpx;
			color: rgba(0, 0, 0, 0.65);
			text-align: left;
		}

		.btn {
			position: absolute;
			bottom: 800rpx;
			width: 670rpx;
			height: 96rpx;
			background: #07C160;
			border-radius: 50rpx;
			font-size: 34rpx;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 96rpx;

			&[disabled] {
				opacity: 0.6;
			}
		}
	}
</style>